package nimo.elibrary.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created with IntelliJ IDEA.
 * User: Enkuso
 * Date: 12/11/12
 * Time: 12:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String TABLE_USER = "user";
    public static final String TABLE_BOOKMARKS = "bookmarks";
    public static final String TABLE_HISTORY = "history";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_BOOKID = "book_id";
    public static final String COLUMN_BOOKTITLE = "book_title";
    public static final String COLUMN_BOOKDESCRIPTION = "book_description";
    public static final String COLUMN_BOOKCOVER = "book_cover";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_USEREMAIL = "email";
    public static final String COLUMN_USERPWD = "password";
    public static final String COLUMN_USERTOKEN = "token";
    public static final String COLUMN_USERFNAME = "firstname";
    public static final String COLUMN_USERLNAME = "lastname";
    public static final String COLUMN_VIEWDATE = "viewed_at";

    private static final String DATABASE_NAME = "nmcbookstore.db";
    private static final int DATABASE_VERSION = 1;

    // Database table creation sql statement
    private static final String BOOKMARK_TABLE_CREATE = "CREATE TABLE "
            + TABLE_BOOKMARKS
            + "("  + COLUMN_ID + " integer primary key autoincrement"
            + ", " + COLUMN_BOOKID + " integer not null"
            + ", " + COLUMN_BOOKTITLE + " text"
            + ", " + COLUMN_BOOKCOVER + " text"
            + ", " + COLUMN_BOOKDESCRIPTION + " text"
            + ");";
    private static final String HISTORY_TABLE_CREATE = "CREATE TABLE "
            + TABLE_HISTORY
            + "("  + COLUMN_ID + " integer primary key autoincrement"
            + ", " + COLUMN_BOOKID + " integer not null"
            + ", " + COLUMN_BOOKTITLE + " text"
            + ", " + COLUMN_BOOKCOVER + " text"
            + ", " + COLUMN_BOOKDESCRIPTION + " text"
            + ", " + COLUMN_VIEWDATE + " date"
            + ");";
    private static final String USER_TABLE_CREATE = "CREATE TABLE "
            + TABLE_USER
            + "("  + COLUMN_ID + " integer primary key autoincrement"
            + ", " + COLUMN_USERNAME + " text"
            + ", " + COLUMN_USEREMAIL + " text"
            + ", " + COLUMN_USERPWD + " text"
            + ", " + COLUMN_USERTOKEN + " text"
            + ", " + COLUMN_USERFNAME + " text"
            + ", " + COLUMN_USERLNAME + " text"
            + ");";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(USER_TABLE_CREATE);
        db.execSQL(HISTORY_TABLE_CREATE);
        db.execSQL(BOOKMARK_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.w(DatabaseHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKMARKS);
        onCreate(db);
    }
}
